#include "GeoPlane.h"

GeoPlane::GeoPlane(float mWidth, float mHeight, int mWSegment, int mHSegment) :Geometry(),m_width(mWidth), m_height(mHeight),
                                                                                m_wSegment(mWSegment),
                                                                                m_hSegment(mHSegment)
{
	this->makeData();
	this->setup();
}

//
void GeoPlane::makeData()
{
	float w_half=m_width/2.0;
	float h_half=m_height/2.0;
	float dx=m_width/m_wSegment;
	float dy=m_height/m_hSegment;
	//计算顶点坐标和纹理坐标
	for(int iy=0;iy<=m_hSegment;++iy)
	{
		float vy=iy*dy-h_half;
		float ty=1-static_cast<float>(iy)/m_hSegment;
		for(int ix=0;ix<=m_wSegment;++ix)
		{
			float vx=ix*dx-w_half;
			float tx=static_cast<float>(ix)/m_wSegment;
			this->vertices.push_back({{vx,vy,0.0},{tx,ty}});
		}
	}
	//计算索引
	for(uint iy=0;iy<m_hSegment;++iy)
		for(uint ix=0;ix<m_wSegment;++ix)
		{
			uint a=iy*(m_wSegment+1)+ix;
			uint b=a+1;
			uint c=(iy+1)*(m_wSegment+1)+ix;
			uint d=c+1;
			this->indices.push_back(a);
			this->indices.push_back(c);
			this->indices.push_back(d);
			this->indices.push_back(a);
			this->indices.push_back(b);
			this->indices.push_back(d);
		}
}